/*
 * @Author: deliteam 462085920@qq.com
 * @Date: 2023-12-12 09:45:01
 * @Description:
 */

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Contact } from './contact.entity';
import { CreateContactDto } from './dto/create-contact.dto';
import { RemoveContactDto } from './dto/remove-contact.dto';
@Injectable()
export class ContactService {
  constructor(
    @InjectRepository(Contact)
    private readonly contactRepository: Repository<Contact>,
  ) {}
  async create(createContactDto: CreateContactDto): Promise<Contact> {
    const { createdAt } = createContactDto;
    createContactDto.createdAt = createdAt || new Date();
    delete createContactDto.id;
    return await this.contactRepository.save(createContactDto);
  }
  // 列表
  async findAll(query: any): Promise<any> {
    const { page = 1, limit = 100 } = query;
    const skip = (page - 1) * limit;
    let params = {
      skip,
      take: limit,
    };
    const [data, total] = await this.contactRepository.findAndCount(params);
    return {
      total,
      data,
    };
  }
  // 删除
  async delete(removeContactDto: RemoveContactDto): Promise<any> {
    const { ids } = removeContactDto;
    return this.contactRepository.delete(ids);
  }
}
